using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Home_ManageUnits : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void fvUnit_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        PlacementManager.UnitComponent oUnitC = new PlacementManager.UnitComponent();
        PlacementManager.Unit oUnit = oUnitC.SelectOne(Int32.Parse(this.gvUnits.SelectedDataKey.Value.ToString()));

        e.NewValues.Add("CreatedBy", oUnit.CreatedBy);
        e.NewValues.Add("DateCreated", oUnit.DateCreated);
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("DivisionId", oUnit.DivisionId);
        e.NewValues.Add("UnitId", oUnit.UnitId);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);
        
    }
    protected void fvUnit_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        this.gvUnits.DataBind();
    }
    protected void fvUnit_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        bool bIsActive = true;
        e.Values.Add("CreatedBy", User.Identity.Name);
        e.Values.Add("DateCreated", DateTime.Now);
        e.Values.Add("DateUpdated", DateTime.Now);
        e.Values.Add("DivisionId", Int32.Parse(Session["DivisionId"].ToString()));
        e.Values.Add("IsActive", bIsActive);
        e.Values.Add("UpdatedBy", User.Identity.Name);
    }
    protected void fvUnit_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
         this.gvUnits.DataBind(); 
    }
    protected void odsUnit_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
    {
        
    }
    protected void gvUnits_DataBound(object sender, EventArgs e)
    {
        if (this.gvUnits.Rows.Count > 0)
        {
            this.fvUnit.ChangeMode(FormViewMode.ReadOnly);
        }
        else
        {
            this.fvUnit.ChangeMode(FormViewMode.Insert);
        }

        this.fvUnit.Focus();
    }
    protected void gvCampuses_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int unitId = Int32.Parse(this.gvUnits.SelectedDataKey.Value.ToString());
            int campusId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CampusId"));

            PlacementManager.LinkUnitCampusComponent oLinkUnitCampusC = new PlacementManager.LinkUnitCampusComponent();
            PlacementManager.LinkUnitCampus oLinkUnitCampus = oLinkUnitCampusC.SelectOneWUnitIdCampusID(unitId, campusId);

            if (oLinkUnitCampus.UnitCampusId > 0)
            {
                ((CheckBox)e.Row.FindControl("cbUnitCampusAssociation")).Checked = oLinkUnitCampus.IsActive;
            }
        }
    }
    protected void btnSaveCampusChanges_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in this.gvCampuses.Rows)
        {
            int index = gvr.RowIndex;
            int campusId = Int32.Parse(this.gvCampuses.DataKeys[index].Value.ToString());
            int unitId = Int32.Parse(this.gvUnits.SelectedDataKey.Value.ToString());
            CheckBox cbUnitCampusAssociation = (CheckBox)gvr.FindControl("cbUnitCampusAssociation");

            PlacementManager.LinkUnitCampusComponent oLinkUnitCampusC = new PlacementManager.LinkUnitCampusComponent();
            PlacementManager.LinkUnitCampus oLinkUnitCampus = oLinkUnitCampusC.SelectOneWUnitIdCampusID(unitId, campusId);

            if (oLinkUnitCampus.UnitCampusId > 0)
            {
                oLinkUnitCampus.DateUpdated = DateTime.Now;
                oLinkUnitCampus.UpdatedBy = User.Identity.Name;
                oLinkUnitCampus.IsActive = cbUnitCampusAssociation.Checked;
                oLinkUnitCampusC.Update(oLinkUnitCampus);
            }
            else
            {
                oLinkUnitCampus.CampusId = campusId;
                oLinkUnitCampus.CreatedBy = User.Identity.Name;
                oLinkUnitCampus.DateCreated = DateTime.Now;
                oLinkUnitCampus.DateUpdated = DateTime.Now;
                oLinkUnitCampus.IsActive = cbUnitCampusAssociation.Checked;
                oLinkUnitCampus.UnitId = unitId;
                oLinkUnitCampus.UpdatedBy = User.Identity.Name;
                oLinkUnitCampusC.Insert(oLinkUnitCampus);
            }
        }

        this.gvCampuses.DataBind();
    }
    protected void gvUnits_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvCampuses.DataBind();
    }
    protected void fvUnit_ModeChanged(object sender, EventArgs e)
    {
        if (fvUnit.CurrentMode == FormViewMode.Insert)
        {
            this.pnlCampusesForUnit.Visible = false;
        }
        else
        {
            this.pnlCampusesForUnit.Visible = true;
        }
    }
}
